class TagsController < ApplicationController

  before_filter :login_required

  require_role ["admin","pdm"], :only => [:fill, :in]

  def index
    @need_to_release_tags = Tag.paginate :page => params[:page], :conditions => ["state = ?", "working"],:order => 'should_release_at ASC', :per_page => 20
    @released_tags = Tag.paginate :page => params[:page], :conditions => ["state = ?", "released"],:order => 'release_at DESC', :per_page => 15
  end

  def show
    @tag = Tag.find(params[:id])
  end

  def fill
    @tag = Tag.find(params[:id])
    @issues = @tag.project.issues
  end

  def in
    @fill_in_data = params[:fill_box_data]
    @tag = Tag.find(params[:id])
    @issue_list = []
    @issue_list = Issue.find(@fill_in_data) unless @fill_in_data.nil?
    @tag.update_by_array(*@issue_list)

    respond_to do |format|
      if @tag.save
        flash[:notice] = 'Tag was successfully created.'
        format.html { redirect_to :controller => :tags, :action => :index }
      else
        format.html { render :action => "new" }
      end
    end
  end

  def change_state
    @tag = Tag.find(params[:id])
    @tag.release_at = Time.now
    @tag.release

    redirect_to tags_path
  end

  def new
    @tag = Tag.new
  end

  def create
    @tag = Tag.new(params[:tag])

    respond_to do |format|
      if @tag.save
        flash[:notice] = 'Tag was successfully created.'
        format.html { redirect_to :controller => :tags, :action => :index }
      else
        format.html { render :action => "new" }
      end
    end
  end

  def edit
    @tag = Tag.find(params[:id])
  end

  def update
    @tag = Tag.find(params[:id])

    respond_to do |format|
      if @tag.update_attributes(params[:tag])
        flash[:notice] = 'Tag was successfully updated.'
        format.html { redirect_to(@tag) }
      else
        format.html { render :action => "edit" }
      end

    end
  end

end
